import React from "react";
import {getBusinessInfo} from 'nc-lightapp-front'
import {getAppPageConfig} from "src/hrpub/common/utils/utils";

export default class MainAction {
    constructor(comp) {
        this.comp = comp;
    }

    appConfig = getAppPageConfig();

    // 获取多语
    getLanguage = (backFn) => {
        const {props} = this.comp;
        const {
            dispatch,
            MultiInit
        } = props;

        MultiInit.getMultiLang({
            moduleId: 'gx6008',
            domainName: 'hrtrn',
            callback: async (json, status, init) => {
                await dispatch({
                    type: 'main/update',
                    payload: {
                        language: json
                    }
                });
                backFn && backFn();
            }
        });
    };

    // 获取模版信息
    getTemplate = () => {
        const {props, action} = this.comp;
        const {
            meta,
            button,
            dispatch,
            createUIDom
        } = props;

        createUIDom(this.appConfig, async (res) => {
            await dispatch({
                type: 'main/update',
                payload: {
                    context: res.context,
                    businessInfo: getBusinessInfo() || {groupId: '', groupName: ''}
                }
            });
            meta.setMeta(this.initMeta(res.template));
            button.setButtons(res.button);
            action.treeAct.getTreeData();
        });
    };

    //初始化模板
    initMeta = (template) => {
        const {props, action} = this.comp;
        const {main, editTable, dispatch} = props;
        const {language} = main;
        template['appconfgrid'].items.forEach(item => {
            item.isSort = false;
            if (item.attrcode === 'unentry_list_show' || item.attrcode === 'manager_list_show') item.visible = false;
        });

        let gridMultiple = {
            ['appconfgrid']: [
                {
                    name: language['gx6008-000230'],//授权状态
                    code: "entryHead",
                    children: ["entry_card_show_edit", "entry_approval", "entry_not_null", "entry_card_show_notedit", "entry_card_notshow"]
                },
                {
                    name: language['gx6008-000230'],//授权状态
                    code: "unentryHead",
                    children: ["unentry_card_show_edit", "unentry_not_null", "unentry_card_show_notedit", "unentry_card_notshow"],
                    visible: false
                },
                {
                    name: language['gx6008-000230'],//授权状态
                    code: "managerHead",
                    children: ["manager_card_notshow"],
                    visible: false
                }
            ]
        };
        props.meta.handleMultiple(template, gridMultiple);
        dispatch({
            type: 'main/update',
            payload: {
                orignalTemplate: template
            }
        });
        return template
    };

    // 初始化
    didMount = () => {
        if (window.location.href.match('localhost:3006')) {
            window.location.hash = `#/ifr?page=20198162232323`;
        }
        this.getLanguage(() => {
            this.getTemplate();
        });
        this.comp.action.btnAct.updateButtonStatus();
    };
}